package com.zztl.infiniteBand.ui.activity

import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.net.requestBean.GetVerifyRequest
import com.zztl.infiniteBand.net.requestBean.IdentityVerifyRequest
import com.zztl.infiniteBand.presenter.RegisterPresenter
import com.zztl.infiniteBand.storage.BillPreference
import com.zztl.infiniteBand.storage.PreferenceKeys
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog
import com.zztl.infiniteBand.ui.view.mvpview.CodeView
import com.zztl.infiniteBand.ui.view.mvpview.RegisterView
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_verify_phone.*
import kotlinx.android.synthetic.main.activity_verify_phone.view.*
import kotlinx.android.synthetic.main.layout_dialog_mention.*

private val s1: String
    get() {
        val code = BillPreference.getApp(PreferenceKeys.COUNTYRCODE.name, "86") as String
        return code
    }

class VerifyPhoneActivity : BaseMvpActivity<RegisterView, RegisterPresenter>(), CodeView {
/*
    @BindView(R.id.tv_phone)
    lateinit var tv_phone: TextView
    @BindView(R.id.btn_send_code)
    lateinit var btn_send_code: TextView
    @BindView(R.id.edit_code)
    lateinit var edit_code: TextView*/

    lateinit var mBaseCodePresenter: RegisterPresenter
    private var mPhone: String? = null
    private var countryCode: String? = null

    private var mExitDialog: ExitDialog? = null
    private var codeNum: String? = null

    override fun setLayoutId(): Int = R.layout.activity_verify_phone


    override fun initBase() {
        mPhone = BillPreference.getApp(PreferenceKeys.DEFAULT_PHONE.name, "") as String
//        countryCode = BillPreference.getApp(PreferenceKeys.COUNTYRCODE.name, "86") as String
        tv_phone.text = mPhone
        edit_code.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                s?.let {
                    btn_next_step.isEnabled = s.length == 4
                }

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })
        //设置EditText的显示方式为多行文本输入
        edit_code.run {
            inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE
            setSingleLine(false)
            setHorizontallyScrolling(false)
        }
    }

    @OnClick(R.id.btn_cacel, R.id.btn_send_code, R.id.btn_next_step)
    fun click(view: View) {
        when (view.id) {
            R.id.btn_cacel -> showExitDialog()
            R.id.btn_send_code -> if (TextUtils.isEmpty(mPhone)) {
                toast(getString(R.string.邮箱不能为空))
            } else {
                mBaseCodePresenter.getCode(GetVerifyRequest(mPhone!!, "6", "" ), this)
                mBaseCodePresenter.startCountDown(this)
            }
            R.id.btn_next_step -> {
                codeNum = edit_code.text.toString().trim { it <= ' ' }
                if (TextUtils.isEmpty(codeNum)) {
                    toast(getString(R.string.验证码不能为空))
                    return
                }
                mBaseCodePresenter.identityVerity(IdentityVerifyRequest(mPhone!!, codeNum!!, "6", ""), this)
            }
        }
    }

    override fun createPresenter(): RegisterPresenter {
        mBaseCodePresenter = RegisterPresenter()
        return mBaseCodePresenter
    }


    override fun getCodeFail() {
        mBaseCodePresenter.resetSmsView()
    }

    override fun setDisableView(count: Int) {
        btn_send_code.isClickable = false
        btn_send_code.text = "(" + count + "S)" + getString(R.string.重新发送)
        btn_send_code.setTextColor(resources.getColor(R.color.color_ACA593))
    }

    override fun setUseableView() {
        btn_send_code.isClickable = true
        btn_send_code.text = getString(R.string.发送验证码)
        btn_send_code.setTextColor(resources.getColor(R.color.color_007aff))
    }

    override fun getCodeSuccess() {
        skipActivity(this, ResetPayPwdActivity::class.java)
    }

    private fun showExitDialog() {
        if (null == mExitDialog) {
            mExitDialog = ExitDialog.Builder(this).setMessage(getString(R.string.是否放弃修改支付密码))
                    .setAffirmTxt(getString(R.string.是))
                    .setCancelTxt(getString(R.string.否))
                    .setClickListener(object : ExitDialog.Builder.ClickListener {
                        override fun cancel() {

                        }

                        override fun comfirm() {
                            finish()
                        }
                    }).create()
        }
        mExitDialog!!.show()
    }


    override fun onDestroy() {
        mBaseCodePresenter.destoryTimer()
        super.onDestroy()
    }
}
